package com.example.fragmentdemo;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.RadioGroup;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    public static final String TAG = "hacket";

    //    @Bind(R.id.radio_group)
    RadioGroup group;

    private FragmentManager manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);
        manager = getSupportFragmentManager();

        group = (RadioGroup) findViewById(R.id.tab_container);

        group.setOnCheckedChangeListener(this);
        //                group.check(R.id.tab1);

        Log.e(TAG, "onCreate: hashCode----" + this.getClass().hashCode() + ",savedInstanceState:" +
                (savedInstanceState == null));

        // 内存重启
        //        if (savedInstanceState != null) {
        //            Log.w(TAG, "MainActivity onCreate savedInstanceState!=null----");
        //            FragmentTransaction ta = manager.beginTransaction();
        //            Fragment f1 = manager.findFragmentByTag("f1");
        //            Fragment f2 = manager.findFragmentByTag("f2");
        //            Fragment f3 = manager.findFragmentByTag("f3");
        //            if (f1 != null) {
        //                ta.hide(f1);
        //            }
        //            if (f2 != null) {
        //                ta.hide(f2);
        //            }
        //            if (f3 != null) {
        //                ta.hide(f3);
        //            }
        //            ta.commit();
        //            Log.i(TAG, "MainActivity onCreate: f1," + (f1 == null) + ",f2:" + (f2 == null) + ",f3:" + (f3
        // == null));
        //        }

        // 监听回退栈变化情况
        manager.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
            @Override
            public void onBackStackChanged() {
                int backStackEntryCount = manager.getBackStackEntryCount();
                List<Fragment> fragments = manager.getFragments();
                Log.i(TAG, "onBackStackChanged,backStackEntryCount:" + backStackEntryCount);
                if (fragments != null) {
                    Log.i(TAG, "onBackStackChanged,fragments size:" + fragments.size() + ",-----" + fragments);
                }
            }
        });

    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        //        manager.popBackStack();
    }

    @OnClick(R.id.pop_back)
    public void pop_back() {
        manager.popBackStack();
    }

    @OnClick(R.id.detail)
    public void detail() {
        startActivity(new Intent(getApplicationContext(), DetailActivity.class));
    }

    private void f1() {
        FragmentTransaction transaction = manager.beginTransaction();
        Fragment f1 = manager.findFragmentByTag("f1");

        //  1、add/show、remove、detach
        if (f1 != null) {
            transaction.show(f1);
            Log.i(TAG, "f1: not null,show()");
            //            transaction.attach(f1);
            //            Log.i(TAG, "f1: not null,attach()");
        } else {
            f1 = new Fragment1fg();
            transaction.add(R.id.framelayout, f1, "f1");
            Log.i(TAG, "f1: is null,add()");
        }

        Fragment f2 = manager.findFragmentByTag("f2");
        if (f2 != null) {
            transaction.hide(f2);   // hide
            Log.i(TAG, "f1: hide f2");

            //            transaction.remove(f2); // remove
            //            Log.i(TAG, "f1: remove f2");

            //            transaction.detach(f2); // detach
        }

        Fragment f3 = manager.findFragmentByTag("f3");
        if (f3 != null) {
            transaction.hide(f3);
            Log.i(TAG, "f1: hide f3");

            //            transaction.remove(f3);
            //            Log.i(TAG, "f1: remove f3");

            //            transaction.detach(f3);
        }

        // 2、replace
        //        transaction.replace(R.id.framelayout, new Fragment1Fg());
        //        Log.i(TAG, "f1: replace() addToBackStack()");

        // 3、add to backstace
        //        transaction.addToBackStack("hacket");
        //        transaction.addToBackStack("hacket");
        //        transaction.addToBackStack("hacket");

        // 4、commit
        int commit = transaction.commit();
        Log.i(TAG, "f1: commit:" + commit);
        //        int i = transaction.commitAllowingStateLoss();

        List<Fragment> fragments = manager.getFragments();
        //        Log.w(TAG, "f1: ------------fragments size--------:" + fragments.size());

    }

    private void f2() {
        FragmentTransaction transaction = manager.beginTransaction();
        Fragment f2 = manager.findFragmentByTag("f2");

        //  1、add/show、remove、detach
        if (f2 != null) {
            transaction.show(f2);
            Log.i(TAG, "f2: not null,show()");

            //            transaction.attach(f2);
            //            Log.i(TAG, "f2: not null,attach()");
        } else {
            f2 = new Fragment2fg();
            transaction.add(R.id.framelayout, f2, "f2");
            Log.i(TAG, "f2: is null,add()");
        }

        Fragment f1 = manager.findFragmentByTag("f1");
        if (f1 != null) {
            transaction.hide(f1);
            Log.i(TAG, "f2: hide f1");

            //            transaction.remove(f1);
            //            Log.i(TAG, "f2: remove f1");

            //            transaction.detach(f1);
        }

        Fragment f3 = manager.findFragmentByTag("f3");
        if (f3 != null) {
            transaction.hide(f3);
            Log.i(TAG, "f2: hide f3");

            //            transaction.remove(f3);
            //            Log.i(TAG, "f2: remove f3");

            //            transaction.detach(f3);
        }

        // 2、replace
        //        transaction.replace(R.id.framelayout, new Fragment2fg());
        //        Log.i(TAG, "f2: replace() addToBackStack()");

        // 3、add to backstace
        //        transaction.addToBackStack("hacket");

        // 4、commit
        int commit = transaction.commit();
        Log.i(TAG, "f2: commit:" + commit);
    }

    private void f3() {
        FragmentTransaction transaction = manager.beginTransaction();
        Fragment f3 = manager.findFragmentByTag("f3");

        //  1、add/show、remove、detach
        if (f3 != null) {
            transaction.show(f3);
            Log.i(TAG, "f3: not null,show()");
            //            transaction.attach(f3);
            //            Log.i(TAG, "f3: not null,attach()");
        } else {
            f3 = new Fragment3fg();
            boolean added = f3.isAdded();
            Log.i(TAG, "f3: added:" + added);
            transaction.add(R.id.framelayout, f3, "f3");
            Log.i(TAG, "f3: is null,add()");
        }

        Fragment f1 = manager.findFragmentByTag("f1");
        if (f1 != null) {
            transaction.hide(f1);
            Log.i(TAG, "f3: hide f1");

            //            transaction.remove(f1);
            //            Log.i(TAG, "f3: remove f1");

            //            transaction.detach(f1);

        }
        Fragment f2 = manager.findFragmentByTag("f2");
        if (f2 != null) {
            transaction.hide(f2);
            Log.i(TAG, "f3: hide f2");

            //            transaction.remove(f2);
            //            Log.i(TAG, "f3: remove f2");

            //            transaction.detach(f2);
        }

        // 2、replace
        //        transaction.replace(R.id.framelayout, new Fragment3fg());
        //        Log.i(TAG, "f3: replace() addToBackStack()");

        // 3、add to backstace
        //        transaction.addToBackStack("hacket");

        // 4、commit
        int commit = transaction.commit();
        Log.i(TAG, "f3: commit:" + commit);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.tab1:
                //                f1();
                f11();
                break;
            case R.id.tab2:
                //                f2();
                f22();
                break;
            case R.id.tab3:
                f3();
                break;
        }
    }

    Fragment f1;
    Fragment f2;

    private void f11() {
        FragmentTransaction transaction = manager.beginTransaction();
        f1 = manager.findFragmentByTag("f1");
        f2 = manager.findFragmentByTag("f2");
        if (f1 == null) {
            this.f1 = new Fragment1fg();
        }
        if (!f1.isAdded()) {
            transaction.add(R.id.framelayout, this.f1, "f1");
        } else {
            transaction.show(f1);
        }
        if (f2 != null && f2.isAdded()) {
            transaction.hide(f2);
        }
        transaction.commit();
    }

    private void f22() {
        FragmentTransaction transaction = manager.beginTransaction();
        f1 = manager.findFragmentByTag("f1");
        f2 = manager.findFragmentByTag("f2");
        if (f2 == null) {
            f2 = new Fragment2fg();
        }
        if (!f2.isAdded()) {
            transaction.add(R.id.framelayout, f2, "f2");
        } else {
            transaction.show(f2);
        }
        if (f1 != null && f1.isAdded()) {
            transaction.hide(f1);
        }
        transaction.commit();
    }

}